$(function(){
    var ajaxFill = new Z.AjaxFill({
        url: Z.url('/backend/entry/records',true),
        templates: {
            list: '<tr>\
                        <td><input type="checkbox" name="id[]" value="${id}"/></td>\
                        <td>${is_display}</td>\
                        <td>${title}</td>\
                        <td>${created_at}</td>\
                        <td class="action">\
                            <a href="${SITE_URL}/index.php/backend/rich_entries/show/${id}/${TYPE}" class="show">查看</a>\
                            <a href="javascript:void(0);" class="edit">编辑</a>\
                            <a href="javascript:void(0);" class="delete">删除</a>\
                            <!--<a href="javascript:void(0);" class="hide">隐藏</a>-->\
                            {{if TYPE==11||TYPE==12}}<a href="javascript:void(0);" class="top">设置头条</a>{{/if}}</div>\
                        </td>\
                    </tr>',
            none: '<tr><td colspan="5" style="text-align:center;">记录为空</td></tr>',
            loading: '<tr><td colspan="5" style="text-align:center;"><img src="/assets/img/backend/loading.gif"/></td></tr>'
        },
        containers: {
            list: $('table.records tbody'),
            pageLinks: $('table.records tfoot td:eq(1)')
        },
        data: {
            type: TYPE,
            pageSize : 10
        }
    });
    
    ajaxFill.request();
    
    $('a.delete').live('click',function(){
        var id = Z.parseInt($(this).parent().siblings().first().find('input:checkbox').val());
        Z.confirm({
            content: 'r u sure?',
            onConfirm: function(){
                Z.ajax({
                    url: Z.url('/backend/entry/delete/'+id,true),
                    success: function(){
                        ajaxFill.reload();
                    }
                });
            }
        });
        return false;
    });
    
    $('a.hide').live('click',function(){
        var id = Z.parseInt($(this).parent().siblings().first().find('input:checkbox').val());
        Z.ajax({
            url: Z.url('/backend/entry/hide/'+id,true),
            success: function(){
                ajaxFill.reload();
            }
        });
        return false;
    });
    
    $('a.top').live('click',function(){
        var id = Z.parseInt($(this).parent().siblings().first().find('input:checkbox').val());
        Z.ajax({
            url: Z.url('/backend/setting/update_by_key',true),
            type: 'post',
            data: {
                value : id,
                key   : TYPE==11?'top_company_news':'top_industry_news'
            },
            success: function(){
                ajaxFill.reload();
            }
        });
        return false;
    });
    
    $('a.select-all').click(function(){
        $('table.records tbody td input:checkbox:not(:checked)').trigger('click');
    });
    
    $('a.select-reverse').click(function(){
        $('table.records tbody td input:checkbox').trigger('click');
    });
    
    $('a.delete-batch').click(function(){
        var ids = [];
        $('table.records tbody td input:checkbox:checked').each(function(){
            ids.push(Z.parseInt($(this).val()));
        });
        Z.confirm({
            content: 'r u sure?',
            onConfirm: function(){
                Z.ajax({
                    url: Z.url('/backend/entry/delete_batch',true),
                    data: {
                        ids:ids
                    },
                    success: function(){
                        ajaxFill.reload();
                    }
                });
            }
        });
    });
    
    $('a.add').click(function(){
        $('table.records').hide();
        $('form').show();
        $('form:eq(0)').attr('action',$('form:eq(0)').attr('action').replace(/\/update$/,'/insert'));
        $('form:eq(0) input[name=title]').val('');
        editor.setContent('');
    });
    
    $('a.edit').live('click',function(){
        $('table.records').hide();
        $('form').show();
        $('form:eq(0)').attr('action',$('form:eq(0)').attr('action').replace(/\/insert$/,'/update'));
        var id = Z.parseInt($(this).parent().siblings().first().find('input:checkbox').val());
        Z.ajax({
            url: Z.url('/backend/entry/find/'+id,true),
            success: function(data){
                Z.fillForm($('form:eq(0)'),data);
                editor.setContent(data.content);
            }
        });
        return false;
    });
    
    $('a.back').click(function(){
        $('table.records').show();
        $('form').hide();
    }).trigger('click');
    
    var editor = new baidu.editor.ui.Editor({
        textarea: 'content'
    }),
    editor_en = new baidu.editor.ui.Editor({
        textarea: 'content_en'
    });
    
    editor.render('content');
    editor_en.render('content_en');
    
    Z.tab($('.tab-button a'), $('.tab-content'), ['active','']);

    $('form').submit(function(){
        editor.sync()
        editor_en.sync();
        Z.ajax({
            url: $(this).attr('action'),
            type: $(this).attr('method'),
            data: $(this).serialize(),
            success: function(){
                ajaxFill.reload();
            }
        });
        return false;
    });
});